package com.ssm.changeunit;

import com.ssm.changeunit.util.FileUtil;
import java.util.*;
import java.io.*;

/**
 * 
 * @author 시리우스
 */
public class Environment {
    /**
     * 모든 속성값을 저장한다..
     */
    public static final String DATABASE_SCHEMA_PATH = "D:\\java\\ChangeUnitSources\\database\\schema.sql";
    
    public static String ROOT_DIR;
    public static String DATABASE_NAME;
    public static String PROPERTIES_PATH;
    public static String USER_ID;
    
    private static final String CURRENT_BRANCH_KEY = "current_branch";

    static {
        System.out.println("!!!!!!!!!!!!!!!!!: " + System.getenv("CHANGEUNIT_ROOT"));
        
        ROOT_DIR = FileUtil.fileSeparatorAddOrNone(System.getenv("CHANGEUNIT_ROOT"));
        DATABASE_NAME = ROOT_DIR + FileUtil.fileSeparatorAddOrNone(".changeunit") + FileUtil.fileSeparatorAddOrNone("db");
        PROPERTIES_PATH = ROOT_DIR + FileUtil.fileSeparatorAddOrNone(".changeunit") + "changeunit.properties";
        
        USER_ID = System.getenv("CHANGEUNIT_USERID");
    }

    public static String getCurrentBranch() throws IOException {
        File configFile = new File(PROPERTIES_PATH);

        FileInputStream fin = new FileInputStream(configFile);

        Properties props = new Properties();
        props.load(new BufferedInputStream(fin));

        fin.close();
        
        return props.getProperty(CURRENT_BRANCH_KEY);
    }

    public static void setCurrentBranch(String currentBranch) throws IOException {
        File configFile = new File(PROPERTIES_PATH);
        
        FileOutputStream fout = new FileOutputStream(configFile);
        
        Properties props = new Properties();
        
        props.setProperty(CURRENT_BRANCH_KEY, currentBranch);
        
        props.store(new BufferedOutputStream(fout), "");

        fout.close();
    }
}

